package com.mind.customer.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.mind.system.domain.AjTicketTask;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.util.List;

/**
 * 单据任务(AjTicketTask)表数据库访问层
 *
 * @author makejava
 * @since 2024-07-23 15:16:24
 */
public interface AjTicketTaskMapper extends BaseMapper<AjTicketTask> {

    @Select("WITH rule_details_manual AS (\n" +
            "    SELECT \n" +
            "        name AS rule_name,\n" +
            "        editor\n" +
            "    FROM \n" +
            "        aj_rule_detail\n" +
            "    WHERE \n" +
            "    name IN (\"保底规则明细\",\"【系统】未上线业务小类人工审核\",\"【系统】混报业务小类人工审核\")\n" +
            ")\n" +
            "\n" +
            "select count(*) from (\n" +
            "SELECT\n" +
            "    t.推单日期,\n" +
            "    t.单据类型,\n" +
            "    tttt.name AS 单据名称,\n" +
            "    t.查验规则数,\n" +
            "    t.查验通过,\n" +
            "    t.查验不通过,\n" +
            "    t.查验不通过_转人工,\n" +
            "    IF(t.查验不通过 = 0, 1, 0) AS 智审一次通过_未剔除转人工规则,\n" +
            "    IF(t.查验不通过_转人工 = t.查验不通过 OR t.查验不通过 = 0, 1, 0) AS 智审一次通过_剔除转人工规则\n" +
            "FROM (\n" +
            "    SELECT\n" +
            "        STR_TO_DATE(a.create_time, '%Y-%m-%d') AS 推单日期,\n" +
            "        a.ticket_type AS 单据类型,\n" +
            "        COUNT(*) AS 查验规则数,\n" +
            "        SUM(CASE WHEN a.status = 1 THEN 1 ELSE 0 END) AS 查验通过,\n" +
            "        SUM(CASE WHEN a.status IN (2, 3) THEN 1 ELSE 0 END) AS 查验不通过,\n" +
            "        SUM(CASE \n" +
            "            WHEN a.status IN (2, 3) AND rd.editor IS NOT NULL \n" +
            "            THEN 1 \n" +
            "            ELSE 0 \n" +
            "        END) AS 查验不通过_转人工\n" +
            "    FROM\n" +
            "        aj_check_result a\n" +
            "    LEFT JOIN\n" +
            "        rule_details_manual rd ON a.rule_name = rd.rule_name\n" +
            "    WHERE\n" +
            "        a.create_time >= #{startDate}\n" +
            "        AND a.create_time < #{endDate}\n" +
            "        AND a.status <> 4\n" +
            "    GROUP BY\n" +
            "        推单日期,\n" +
            "        单据类型,\n" +
            "        ticket_id\n" +
            ") t\n" +
            "LEFT JOIN\n" +
            "    aj_ticket_type tttt ON t.单据类型 = tttt.code\n" +
            ") X where X.查验不通过_转人工 = 1 ")
    int selectByDate(@Param("startDate") String startDate, @Param("endDate") String endDate);

    /**
     * 查询单据任务数据集合
     * 
     * @param ticketTask 单据任务信息
     * @return 单据任务数据集合
     */
    public List<AjTicketTask> selectTicketTaskList(AjTicketTask ticketTask);
    public List<AjTicketTask> selectTicketOnlyHignestVersion(AjTicketTask ticketTask);

    /**
     * 通过单据任务ID查询单据任务信息
     * 
     * @param ticketTaskId 单据任务ID
     * @return 角色对象信息
     */
    public AjTicketTask selectTicketTaskById(Long ticketTaskId);

    /**
     * 删除单据任务信息
     * 
     * @param ticketTaskId 单据任务ID
     * @return 结果
     */
    public int deleteTicketTaskById(Long ticketTaskId);

    /**
     * 批量删除单据任务信息
     * 
     * @param ticketTaskIds 需要删除的单据任务ID
     * @return 结果
     */
    public int deleteTicketTaskByIds(Long[] ticketTaskIds);

    /**
     * 修改单据任务信息
     * 
     * @param ticketTask 单据任务信息
     * @return 结果
     */
    public int updateTicketTask(AjTicketTask ticketTask);

    /**
     * 新增单据任务信息
     * 
     * @param ticketTask 单据任务信息
     * @return 结果
     */
    public int insertTicketTask(AjTicketTask ticketTask);
    
    /**
     * 批量新增单据任务信息
     * 
     * @param ticketTaskList 单据任务信息
     * @return 结果
     */
    public int insertTicketTaskBatch(@Param("entities") List<AjTicketTask> ticketTaskList);
    
    
    /**
     * 根据名称查单据任务
     *
     * @param name 单据任务名称
     * @return 结果
     */
    AjTicketTask selectTicketTaskByName(String name);

    AjTicketTask pullTask();

    AjTicketTask selectByVersion(@Param("ticketId") String ticketId, @Param("version") Integer version);


    /**
     *
     * @param ticketId
     * @return
     */
    AjTicketTask selectLastVersionTask(String ticketId);

    List<AjTicketTask> selectByStatusList(@Param("ticketId") String ticketId, @Param("statusList") List<Integer> statusList);

    AjTicketTask selectTicketTaskByVersion(@Param("ticketId") String ticketId, @Param("version") Integer version);


    /**
     *
     * @param ticketId
     * @return
     */
    AjTicketTask selectTicketTaskByTicketId(String ticketId);
}

